package com.pzh.assignment.controller;

import com.pzh.assignment.common.bean.Result;
import com.pzh.assignment.common.bean.param.BindMenusParam;
import com.pzh.assignment.entity.RoleMenu;
import com.pzh.assignment.service.IRoleMenuService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pengzhihao
 * @since 2022-04-06
 */
@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {

    @Autowired
    IRoleMenuService roleMenuService;

    /**
     * @description:  绑定菜单到角色
     * @param: param
     * @return: com.pzh.assignment.common.bean.Result
     * @author pengzhihao
     * @date: 2022/4/7
     */
    @RequiresRoles(value = {"sys_admin"}, logical = Logical.OR)
    @PostMapping
    public Result bindMenus(@Validated @RequestBody BindMenusParam param) {
        List<RoleMenu> roleMenuList = new ArrayList<>();
        for (Integer menuId : param.getMenuIds()) {
            roleMenuList.add(RoleMenu.valueOf(menuId, param.getRoleId()));
        }
        roleMenuService.saveBatch(roleMenuList);
        return Result.success(null);
    }
}
